package com.jeesite.modules.kj.api.controller.member;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.common.interfaces.CurrentUser;
import com.jeesite.modules.common.interfaces.UserLoginToken;
import com.jeesite.modules.kj.api.vo.DataListPageVo;
import com.jeesite.modules.kj.api.vo.request.IntegralAuthRequestVo;
import com.jeesite.modules.kj.api.vo.response.*;
import com.jeesite.modules.kj.api.vo.responseVo.ResponseBean;
import com.jeesite.modules.kj.entity.*;
import com.jeesite.modules.kj.exception.FreezMemberException;
import com.jeesite.modules.kj.service.KjIntegralAuthService;
import com.jeesite.modules.kj.service.KjServiceTypeService;
import com.jeesite.modules.kj.utils.KjMemberUtil;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.utils.DictUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Calendar;
import java.util.List;

/**
 * Author:hzy
 * Date:2021/07/04
 * Description:
 */
@Api(value = "认证api", tags = {"认证API"})
@Slf4j
@Controller
@RequestMapping(value = "/api/integralAuth")
public class IntegralAuthApiController {
    private final static Logger logger = LoggerFactory.getLogger(IntegralAuthApiController.class);

    @Autowired
    KjIntegralAuthService kjIntegralAuthService;

    @Autowired
    KjServiceTypeService kjServiceTypeService;

    @ApiOperation(value = "获取服务类型列表", notes = "获取服务类型列表")
    @GetMapping(value = "serviceTypeList")
    public ResponseBean<List<ServiceTypeResponseVo>> serviceTypeList() {
//        List<DictData> dictList = DictUtils.getDictList("service_type");
        List<KjServiceType> typeList = kjServiceTypeService.findList(new KjServiceType());
        List<ServiceTypeResponseVo> vos = ListUtils.newArrayList();
        for (KjServiceType dict: typeList
        ) {
            vos.add(new ServiceTypeResponseVo(dict));
        }
        return new ResponseBean<>(vos);
    }


    @ApiOperation(value = "添加认证", notes = "添加认证")
    @PostMapping(value = "create")
    @UserLoginToken
    public ResponseBean<String> create(@ApiIgnore @CurrentUser String memberId, IntegralAuthRequestVo vo) throws FreezMemberException {
        KjIntegralAuth auth = vo.convertToEntity();
        KjMember kjMember = KjMemberUtil.getCurrentMember(memberId);
        auth.setKjMember(kjMember);
        kjIntegralAuthService.save(auth);
        return new ResponseBean<>(Global.TRUE, auth.getId());
    }



    @ApiOperation(value = "修改认证", notes = "修改认证")
    @PostMapping(value = "update")
    @UserLoginToken
    public ResponseBean<String> update(@ApiIgnore @CurrentUser String memberId, IntegralAuthRequestVo vo) throws FreezMemberException {
        KjIntegralAuth auth = vo.convertToEntity();
        KjMember kjMember = KjMemberUtil.getCurrentMember(memberId);
        auth.setKjMember(kjMember);
        auth.setApproveStatus(KjIntegralAuth.PROCESSING);
        auth.setApproveRemarks("");
        kjIntegralAuthService.save(auth);
        return new ResponseBean<>(Global.TRUE, auth.getId());
    }
    @ApiOperation(value = "认证详情", notes = "认证详情")
    @GetMapping(value = "{id}")
    public ResponseBean<IntegralAuthResponseVo> detail(@PathVariable(name = "id") String id) {
        KjIntegralAuth kjIntegralAuth = kjIntegralAuthService.get(id);
        return new ResponseBean<>(new IntegralAuthDetailResponseVo(kjIntegralAuth));
    }



    @ApiOperation(value = "我认证的服务", notes = "我认证的服务")
    @GetMapping(value = "my")
    @UserLoginToken
    public ResponseBean<DataListPageVo<IntegralAuthResponseVo>> my(@ApiIgnore @CurrentUser String memberId, String year, String month, int pageNo, int pageSize) throws FreezMemberException {
        KjIntegralAuth where = new KjIntegralAuth();
        KjMember kjMember = KjMemberUtil.getCurrentMember(memberId);
        where.setKjMember(kjMember);
        if(StringUtils.isNotBlank(year) && Integer.parseInt(year) != 0 && StringUtils.isNotBlank(month) && Integer.parseInt(month) != 0){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
            where.setCreateDate_gte(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
            calendar.add(Calendar.SECOND, -1);
            where.setCreateDate_lte(calendar.getTime());
        }
        where.setPage(new Page<>(pageNo, pageSize));
        Page<KjIntegralAuth> results = kjIntegralAuthService.findPage(where);
        DataListPageVo<IntegralAuthResponseVo> vos = new DataListPageVo<>(results, IntegralAuthResponseVo.class);
        return new ResponseBean<>(vos);
    }
}
